package gravity;
import gravity.test.particle;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

import javax.imageio.ImageIO;
@SuppressWarnings("unused")
public class draw
{
	public static String fileGen(int frame)
	{
		String rv = Integer.toString(frame);
		while (rv.length() < 4)
		{
			rv = "0"+rv;
		}
		rv = "data/"+rv+".dat";
		return rv;
	}
	public static void write(BufferedImage image, int frame) throws Exception
	{
		String filename = Integer.toString(frame);
		while (filename.length() < 4)
		{
			filename = "0"+filename;
		}
		filename = "img/"+filename+".png";
		File file = new File(filename);
		ImageIO.write(image, "png", file);
	}
	public static particle[] readData(int frame) throws Exception
	{
		String filename = fileGen(frame);
		FileInputStream fis = new FileInputStream(filename);
		ObjectInputStream in = new ObjectInputStream(fis);
		particle[] rv = (particle[])in.readObject();
		in.close();
		return rv;
	}
	public static double[] convert(double[] pos, int height, int width)
	{
		double[] rv = {width/2.0, height/2.0};
		rv[0] = rv[0] - (Math.sqrt(3)*pos[0]/2.0) + (Math.sqrt(3)*pos[1]/2.0);
		rv[1] = rv[1] - pos[2] - pos[0]/2 - pos[1]/2;
		return rv;
	}
	public static void main(String[] args) throws Exception
	{
		/*int width = 1920;
		int height = 1080;
		int totalFrames = 6000;
		int totalParticles = 10000;
		particle[] particleList;
		double[] center = new double[2];
		int xs, xf, ys, yf;
		BufferedImage obj = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for (int frame = 0; frame < totalFrames; frame++)
		{
			particleList = readData(frame);
			Graphics2D img = obj.createGraphics();
			for (int i = 0; i < totalParticles; i++)
			{
				if (particleList[i].mass != 0)
				{
					img.setColor(new Color((int)particleList[i].color[0],(int)particleList[i].color[1], (int)particleList[i].color[2]));
					center = convert(particleList[i].position, height, width);
					xs = (int) Math.round(center[0] - particleList[i].radius);
					xf = (int) Math.round(center[0] + particleList[i].radius);
					ys = (int) Math.round(center[1] - particleList[i].radius);
					yf = (int) Math.round(center[1] + particleList[i].radius);
					img.fill(new Ellipse2D.Float(xs, xf, ys, yf));
					continue;
				}
				break;
			}
			write(obj, frame);
			System.out.println(frame);
			img.dispose();
		}*/
		double[] pos = {1.0,1.0,0.0};
		System.out.println(convert(pos, 100, 100)[0]);
		System.out.println(convert(pos, 100, 100)[1]);
	}
}